package com.loongtech.bi.action.biplayer;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.entity.system.EntityBiChannel;
import com.loongtech.bi.entity.system.EntityBiPlatform;
import com.loongtech.bi.entity.system.EntityBiServer;
import com.loongtech.bi.manager.player.AlivePlayerManager;
import com.loongtech.bi.manager.player.AlivePlayerManager.AlivePlayerData;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 玩家分析-留存分析Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/player/alivePlayer")
public class AlivePlayerBiAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	private List<AlivePlayerData> alivePlayerDatas;  // 留存数据
	private ChartUIData alivePlayerChart;  // 留存图

    private String selectedType = "1";  // 1=新增设备 2=新增帐号 3=活跃帐号 4=首付帐号

    private Date schoolDate = new Date(Time.getBeginningOfDay(Time.currentTime() - 2 * Time.DAY));
    private List<AlivePlayerManager.RoleSchoolLerfData> aliveRoleSchoolDatas;  // 留存数据
    private ChartUIData aliveRoleSchoolChart;  // 留存图
    private String queryPlatform2 = "0"; // 前端传过来的查询条件：平台
    private String queryChannel2 = "0"; // 前端传过来的查询条件：渠道
    private String queryServer2 = "0"; // 前端传过来的查询条件：区服
    private Map<String, String> platformItmes2; // 平台下拉列表
    private Map<String, String> channelItmes2; // 渠道下拉列表
    private Map<String, String> serverItmes2; // 区服下拉列表

	@Autowired
	AlivePlayerManager alivePlayerManager;
	
    @RequestMapping(value = "initParam.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void initParam(HttpServletRequest request, HttpServletResponse response, String callback) {
        try {
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);

            initData2();
            result.put("serverItmes2", serverItmes2);
            result.put("channelItmes2", channelItmes2);
            result.put("platformItmes2", platformItmes2);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, @RequestParam String platform, @RequestParam String channel, @RequestParam String server, @RequestParam String selectedType, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            dateStart = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            dateEnd = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);
            queryPlatform = platform;
            queryChannel = channel;
            queryServer = server;

            alivePlayerDatas = new ArrayList<>();
            alivePlayerChart = null;

            alivePlayerDatas = alivePlayerManager.getAllData(dateStart, dateEnd, platform, channel, server, selectedType);
            if (alivePlayerDatas != null && alivePlayerDatas.size() > 0) {
                alivePlayerChart = new ChartUIData();
                alivePlayerChart.setJsonType(ChartType.COLUMN.type);
                alivePlayerChart.setJsonStr(new ChartTool(alivePlayerDatas, AlivePlayerData.class, "").loadData(alivePlayerChart.getJsonType()));

                AlivePlayerData z = alivePlayerManager.new AlivePlayerData();
                z.setDateStr("平均数");
                z.setPlatformId(queryPlatform);
                z.setChannelId(queryChannel);
                z.setServerId(queryServer);
                int nu = 0;
                int t1 = 0;
                int t2 = 0;
                int t3 = 0;
                int t4 = 0;
                int t5 = 0;
                int t6 = 0;
                int t7 = 0;
                int t14 = 0;
                int t30 = 0;
                int t60 = 0;
                int t90 = 0;
                int t120 = 0;

                for (AlivePlayerData t : alivePlayerDatas) {
                    if (t.getNewUser() > 0) {
                        nu++;
                        z.setNewUser(t.getNewUser() + z.getNewUser());
                    }
                    if (z.getThe2Left() > 0) {
                        t2++;
                        z.setThe2Left(t.getThe2Left() + z.getThe2Left());
                    }
                    if (z.getThe3Left() > 0) {
                        t3++;
                        z.setThe3Left(t.getThe3Left() + z.getThe3Left());
                    }
                    if (z.getThe4Left() > 0) {
                        t4++;
                        z.setThe4Left(t.getThe4Left() + z.getThe4Left());
                    }
                    if (z.getThe5Left() > 0) {
                        t5++;
                        z.setThe5Left(t.getThe5Left() + z.getThe5Left());
                    }
                    if (z.getThe6Left() > 0) {
                        t6++;
                        z.setThe6Left(t.getThe6Left() + z.getThe6Left());
                    }
                    if (z.getThe7Left() > 0) {
                        t7++;
                        z.setThe7Left(t.getThe7Left() + z.getThe7Left());
                    }
                    if (z.getThe14Left() > 0) {
                        t14++;
                        z.setThe14Left(t.getThe14Left() + z.getThe14Left());
                    }
                    if (z.getThe30Left() > 0) {
                        t30++;
                        z.setThe30Left(t.getThe30Left() + z.getThe30Left());
                    }
                    if (z.getThe60Left() > 0) {
                        t60++;
                        z.setThe60Left(t.getThe60Left() + z.getThe60Left());
                    }
                    if (z.getThe90Left() > 0) {
                        t90++;
                        z.setThe90Left(t.getThe90Left() + z.getThe90Left());
                    }
                    if (z.getThe120Left() > 0) {
                        t120++;
                        z.setThe120Left(t.getThe120Left() + z.getThe120Left());
                    }
                }
                if(nu > 0) {
                    z.setNewUser(z.getNewUser() / nu);
                }
                if(t1 > 0) {
                    z.setThe2Left(z.getThe2Left() / t1);
                }
                if(t2 > 0) {
                    z.setThe2Left(z.getThe2Left() / t2);
                }
                if(t3 > 0) {
                    z.setThe3Left(z.getThe3Left() / t3);
                }
                if(t4 > 0) {
                    z.setThe4Left(z.getThe4Left() / t4);
                }
                if(t5 > 0) {
                    z.setThe5Left(z.getThe5Left() / t5);
                }
                if(t6 > 0) {
                    z.setThe6Left(z.getThe6Left() / t6);
                }
                if(t7 > 0) {
                    z.setThe7Left(z.getThe7Left() / t7);
                }
                if(t14 > 0) {
                    z.setThe14Left(z.getThe14Left() / t14);
                }
                if(t30 > 0) {
                    z.setThe30Left(z.getThe30Left() / t30);
                }
                if(t60 > 0) {
                    z.setThe60Left(z.getThe60Left() / t60);
                }
                if(t90 > 0) {
                    z.setThe90Left(z.getThe90Left() / t90);
                }
                if(t120 > 0) {
                    z.setThe120Left(z.getThe120Left() / t120);
                }
                alivePlayerDatas.add(0, z);
            }

            result.put("res", "0");
            result.put("alivePlayerDatas", alivePlayerDatas);
            result.put("alivePlayerChart", alivePlayerChart);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
	}

    @RequestMapping(value = "search2.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search2(HttpServletRequest request, HttpServletResponse response, @RequestParam String schoolDate, @RequestParam String platform, @RequestParam String channel, @RequestParam String server, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            Date date = Time.formatDate(schoolDate, Time.TIMEPATTERN_SIMPLE);

            aliveRoleSchoolDatas = alivePlayerManager.getAllRoleSchoolData(date, platform, channel, server);
            List<AlivePlayerManager.RoleSchoolLeftChartData> list = alivePlayerManager.getRoleSchoolleftChartData(aliveRoleSchoolDatas);
            if (list != null && list.size() > 0) {
                aliveRoleSchoolChart = new ChartUIData();
                aliveRoleSchoolChart.setJsonType(ChartType.SPLINE.type);
                aliveRoleSchoolChart.setJsonStr(new ChartTool(list, AlivePlayerManager.RoleSchoolLeftChartData.class, "").loadData(aliveRoleSchoolChart.getJsonType(), true));
            } else {
                aliveRoleSchoolChart = null;
            }

            result.put("res", "0");
            result.put("aliveRoleSchoolDatas", aliveRoleSchoolDatas);
            result.put("aliveRoleSchoolChart", aliveRoleSchoolChart);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }

    @RequestMapping(value = "updateServer.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateServer(HttpServletRequest request, HttpServletResponse response, String server, String callback) {
        try {
            queryServer = server;
            selectedServer();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updateChannel.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateChannel(HttpServletRequest request, HttpServletResponse response, String channel, String callback) {
        try {
            queryChannel = channel;
            updateServerItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updatePlatform.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updatePlatform(HttpServletRequest request, HttpServletResponse response, String platform, String callback) {
        try {
            queryPlatform = platform;
            updateChannelItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updateServer2.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateServer2(HttpServletRequest request, HttpServletResponse response, String server, String callback) {
        try {
            queryServer2 = server;
            selectedServer2();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("channelItmes", channelItmes2);
            result.put("platformItmes", platformItmes2);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updateChannel2.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateChannel2(HttpServletRequest request, HttpServletResponse response, String channel, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            queryChannel2 = channel;
            updateServerItmes2();
            result.put("res", "0");
            result.put("serverItmes", serverItmes2);
            result.put("platformItmes", platformItmes2);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updatePlatform2.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updatePlatform2(HttpServletRequest request, HttpServletResponse response, String platform, String callback) {
        try {
            queryPlatform2 = platform;
            updateChannelItmes2();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes2);
            result.put("channelItmes", channelItmes2);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    public void initData2() {
        platformItmes2 = new LinkedHashMap<>();
        platformItmes2.put("全平台", "0");
        for (EntityBiPlatform p : BiPlatformManager.PLATFORM_LIST) {
            platformItmes2.put(p.getName(), p.getId());
        }
        updateChannelItmes2();
    }

    public void updateChannelItmes2() {
        if (!"0".equals(queryPlatform2)) {
            channelItmes2 = new LinkedHashMap<>();
            channelItmes2.put("全渠道", "0");
            serverItmes2 = new LinkedHashMap<>();
            serverItmes2.put("全区服", "0");

            return;
        }

        List<EntityBiChannel> list = BiPlatformManager.getChannelIdsByPlatformId(queryPlatform2);

        channelItmes2 = new LinkedHashMap<>();
        channelItmes2.put("全渠道", "0");
        for (EntityBiChannel c : list) {
            channelItmes2.put(c.getName(), c.getId());
        }

        updateServerItmes2();
    }

    public void updateServerItmes2() {
        if (!"0".equals(queryChannel2)) {
            platformItmes2 = new LinkedHashMap<>();
            platformItmes2.put("全平台", "0");
            serverItmes2 = new LinkedHashMap<>();
            serverItmes2.put("全区服", "0");

            return;
        }

        platformItmes2 = new LinkedHashMap<>();
        platformItmes2.put("全平台", "0");
        for (EntityBiPlatform p : BiPlatformManager.PLATFORM_LIST) {
            platformItmes2.put(p.getName(), p.getId());
        }

        List<EntityBiServer> list = BiPlatformManager.SERVER_LIST;

        serverItmes2 = new LinkedHashMap<>();
        serverItmes2.put("全区服", "0");
        for (EntityBiServer s : list) {
            serverItmes2.put(s.getName(), s.getId());
        }
    }

    public void selectedServer2() {
        if (!"0".equals(queryServer2)) {
            platformItmes2 = new LinkedHashMap<>();
            platformItmes2.put("全平台", "0");
            channelItmes2 = new LinkedHashMap<>();
            channelItmes2.put("全渠道", "0");

            return;
        }
        initData2();
    }

    public void search() {
    }

	@Override
	protected String csvContent() {
		return null;
	}

	@Override
    protected String csvFileName() {
	    return "留存分析";
    }

    public List<AlivePlayerData> getAlivePlayerDatas() {
        return alivePlayerDatas;
    }

    public void setAlivePlayerDatas(List<AlivePlayerData> alivePlayerDatas) {
        this.alivePlayerDatas = alivePlayerDatas;
    }

    public ChartUIData getAlivePlayerChart() {
        return alivePlayerChart;
    }

    public void setAlivePlayerChart(ChartUIData alivePlayerChart) {
        this.alivePlayerChart = alivePlayerChart;
    }

    public String getSelectedType() {
        return selectedType;
    }

    public void setSelectedType(String selectedType) {
        this.selectedType = selectedType;
    }

    public Date getSchoolDate() {
        return schoolDate;
    }

    public void setSchoolDate(Date schoolDate) {
        this.schoolDate = schoolDate;
    }

    public List<AlivePlayerManager.RoleSchoolLerfData> getAliveRoleSchoolDatas() {
        return aliveRoleSchoolDatas;
    }

    public void setAliveRoleSchoolDatas(List<AlivePlayerManager.RoleSchoolLerfData> aliveRoleSchoolDatas) {
        this.aliveRoleSchoolDatas = aliveRoleSchoolDatas;
    }

    public ChartUIData getAliveRoleSchoolChart() {
        return aliveRoleSchoolChart;
    }

    public void setAliveRoleSchoolChart(ChartUIData aliveRoleSchoolChart) {
        this.aliveRoleSchoolChart = aliveRoleSchoolChart;
    }

    public String getQueryPlatform2() {
        return queryPlatform2;
    }

    public void setQueryPlatform2(String queryPlatform2) {
        this.queryPlatform2 = queryPlatform2;
    }

    public String getQueryChannel2() {
        return queryChannel2;
    }

    public void setQueryChannel2(String queryChannel2) {
        this.queryChannel2 = queryChannel2;
    }

    public String getQueryServer2() {
        return queryServer2;
    }

    public void setQueryServer2(String queryServer2) {
        this.queryServer2 = queryServer2;
    }

    public Map<String, String> getPlatformItmes2() {
        return platformItmes2;
    }

    public void setPlatformItmes2(Map<String, String> platformItmes2) {
        this.platformItmes2 = platformItmes2;
    }

    public Map<String, String> getChannelItmes2() {
        return channelItmes2;
    }

    public void setChannelItmes2(Map<String, String> channelItmes2) {
        this.channelItmes2 = channelItmes2;
    }

    public Map<String, String> getServerItmes2() {
        return serverItmes2;
    }

    public void setServerItmes2(Map<String, String> serverItmes2) {
        this.serverItmes2 = serverItmes2;
    }
}
